package ldkxingzhe.ldkxingzhedemo.view.listview;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;

import ldkxingzhe.ldkxingzhedemo.R;
import ldkxingzhe.ldkxingzhedemo.utils.LogDemo;

/**
 * Created by xingzhe on 15/10/8.
 */
public class MultipleRowTypeListViewActivity extends AppCompatActivity{

    static final String TAG = MultipleRowTypeListViewActivity.class.getSimpleName();

    private ListView listView;


    private String names[] = {
            "机设112","机制113","机自113","理验2班"
    };

    private String classmates[][] = {
            {"唐辉","黄吉民","马武超","石旭"},
            {"王进要","续泽","施再峰","宋旭辉"},
            {"孙嘉宾","张文涛","赵志豪","赵高权"},
            {"翟荧辉","陈玉君","王静","陈友军"}
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_multiple_row_type_list_view);
        listView = (ListView)findViewById(R.id.list_view);
        listView.setAdapter(myAdapter);
        listView.setOnItemClickListener(myAdapter);
    }

    private MyBaseAdapter myAdapter = new MyBaseAdapter();
    class MyBaseAdapter extends BaseAdapter implements ListView.OnItemClickListener {
        @Override
        public int getCount() {
            int count = 0;
            for(String[] batch : classmates){
                count += 1 + batch.length;
            }
            return count;
        }

        @Override
        public Object getItem(int position) {
            int offset = position;
            int batchIndex = 0;

            for(String[] batch : classmates){
                if(offset == 0){
                    return Integer.valueOf(batchIndex);
                }

                offset--;
                if(offset < batch.length){
                    return batch[offset];
                }

                offset -= batch.length;
                batchIndex++;
            }
            throw new IllegalStateException("Invalid position #" + position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            if(getItemViewType(position) == 0){
                return getHeaderView(position,convertView,parent);
            }

            if(convertView == null){
                LayoutInflater inflater = getLayoutInflater();
                convertView = inflater.inflate(R.layout.listview_simple_contat,parent,false);
            }
            TextView name = (TextView)convertView.findViewById(R.id.name);
            name.setText((String)getItem(position));
            return convertView;
        }

        @Override
        public int getViewTypeCount() {
            return names.length;
        }

        /** 0代表是头, 1代表是子 */
        @Override
        public int getItemViewType(int position) {
            if(getItem(position) instanceof Integer){
                return 0;
            }
            return 1;
        }

        private View getHeaderView(int position, View convertView, ViewGroup parent){
            if(convertView == null){
                LayoutInflater layoutInflater = getLayoutInflater();
                convertView = layoutInflater.inflate(R.layout.list_view_context_menu,parent,false);
            }
            TextView text = (TextView)convertView.findViewById(R.id.text);
            text.setText(names[(Integer) getItem(position)]);
            return convertView;
        }

        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            LogDemo.v(TAG,"position was click");
            if(getItem(position) instanceof Integer){
                LogDemo.v(TAG,"header #" + (Integer)getItem(position) + " is " + names[(Integer)getItem(position)]);
            }else{
                LogDemo.v(TAG,"items is #" + (String)getItem(position));
            }
        }
    };
}

